package com.itboye.ihomebank.key;

import java.util.Observable;
import java.util.Observer;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout.LayoutParams;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.ToggleButton;

import com.itboye.ihomebank.R;
import com.itboye.ihomebank.base.BaseOtherActivity;
import com.itboye.ihomebank.bean.ResultEntity;
import com.itboye.ihomebank.constants.SPContants;
import com.itboye.ihomebank.db.KeyService;
import com.itboye.ihomebank.db.MyKeyService;
import com.itboye.ihomebank.presenter.KeyPresenter;
import com.itboye.ihomebank.presenter.UserPresenter;
import com.itboye.ihomebank.util.ByAlert;
import com.itboye.ihomebank.util.SPUtils;

/**
 * 管理员用户(锁设置)
 * 
 */
public class ActivityKeySheZhiTwo extends BaseOtherActivity implements Observer {
	private ImageView close_icon;// 返回图片
	private TextView add_shap_title_tv;// 标题
	TextView shezhi_two_name;//锁别名
	ToggleButton shezhi_two_tb;//开锁提醒
	private String lockId;
	private String keyId;
	private String uid;
	private String lockAlias;
	public static boolean addLock = false;
	private Dialog dialog;// 自定义Dialog
	RelativeLayout guanli_pass;
	TextView shezhi_two_pass;//管理员密码
	RelativeLayout bieming;//锁别名
	
	UserPresenter userPresenter;
	KeyPresenter keyPresenter;
	
	public static String LOCKALILAS = "com.zhujia.keyname";
	KeyService keyService;
	MyKeyService mykeyService;
	
	private String keyName;
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.close_icon:
			finish();
			break;
		case R.id.bieming:
			dialog.show();
			break;
		case R.id.shezhi_two_tb:// 密码可见
			if (shezhi_two_tb.isChecked()) {
				keyPresenter.getTuiSong(uid, lockId, 1+"");
			} else {
				keyPresenter.getTuiSong(uid, lockId, 0+"");

			}
			
		default:
			break;
		}

	}

	@Override
	protected int layout_id() {
		return R.layout.activity_key_shezhi_two;
	}

	@Override
	protected void onInitController() {

	}

	@Override
	protected void onInit() {
		add_shap_title_tv.setText("设置");
		keyPresenter=new KeyPresenter(this);
		mykeyService=new MyKeyService(this);
		lockId = getIntent().getStringExtra("lockid");
		keyId=getIntent().getStringExtra("keyid");
		lockAlias= getIntent().getStringExtra("lockAlias");
		shezhi_two_name.setText(lockAlias);
		
		uid = SPUtils.get(this, null, SPContants.USER_ID, "") + "";
		userPresenter = new UserPresenter(this);
		
		showExtraDialog();
	}

	@Override
	protected void onReadIntent(Bundle bundle) {

	}
	/**
	 * 自定义Dialog
	 */
	private void showExtraDialog() {
		dialog = new Dialog(ActivityKeySheZhiTwo.this, R.style.rel_dialog);
		View view = LayoutInflater.from(ActivityKeySheZhiTwo.this).inflate(
				R.layout.user_shezhi, null);
		dialog.setContentView(view);
		
		final EditText dialog_et = (EditText) view.findViewById(R.id.dialog_et);
		
		TextView dialog_ok = (TextView) view
				.findViewById(R.id.dialog_ok);
		TextView dialog_quxiao = (TextView) view
				.findViewById(R.id.dialog_quxiao);
		dialog.setCanceledOnTouchOutside(false);
		dialog_ok.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				keyName=dialog_et.getText().toString();
				userPresenter.updateKey(uid, lockId, keyName);
				showProgressDialog("修改中,请稍后", true);
				dialog.dismiss();
			}
		});
		dialog_quxiao.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				dialog.dismiss();
			}
		});
		WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
		Display display = wm.getDefaultDisplay();
		// 屏幕宽度
		int screenWidth = display.getWidth();
		// 屏幕高度
		int screenHeight = display.getHeight();
		Window window = dialog.getWindow();

		WindowManager.LayoutParams lp = window.getAttributes();
		window.setGravity(Gravity.CENTER);
		lp.width = screenWidth * 3 / 4;
		lp.height = LayoutParams.WRAP_CONTENT;
		window.setAttributes(lp);

	}
	@Override
	public void update(Observable observable, Object data) {
		ResultEntity entity = handlerError(data);
		if (entity != null) {
			if (entity.getEventType() == UserPresenter.updateKey_success) {
				ByAlert.alert(entity.getData());
				keyService.deleteData();//修改成功后清空本地存在的备份钥匙，重新加载()
				mykeyService.deleteData();//修改成功后清空本地存在的钥匙，重新加载()
			}
			if (entity.getEventType() == UserPresenter.updateKey_fail) {
				ByAlert.alert(entity.getData());
			}
			if (entity.getEventType() == KeyPresenter.tuisong_success) {
				ByAlert.alert(entity.getData());
			}
			if (entity.getEventType() == KeyPresenter.tuisong_fail) {
				ByAlert.alert(entity.getData());
			}
		}
		try {
			closeProgressDialog();
		} catch (Exception e) {
		}
	}

}
